package com.bdqn.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.dao.SettingMapper;
import com.bdqn.bookshop.entity.Setting;
import com.bdqn.bookshop.service.SettingService;
import com.bdqn.bookshop.utils.FileResult;
import com.bdqn.bookshop.utils.FileUtil;
import com.bdqn.bookshop.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 16:03
 **/
@Service
@Transactional
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {

    @Override
    public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request) {
        String newFileName = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"_temp";
        Map<String,Object> map = new HashMap<String,Object>();
        FileResult fr = FileUtil.saveFileToPath(request,file, BookConstants.SETTING_SAVE_PATH,BookConstants.SETTING_MAPPING_URL,newFileName);
        if(fr!=null){
            List<String> list = (List<String>) request.getSession().getAttribute("settingImage");
            if(list==null){
                list = new ArrayList<String>();
            }
            list.add(fr.getFileName());
            request.getSession().setAttribute("settingImage",list);
            map.put("code", 0);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("src", fr.getHttpUrl());
            map.put("data", map1);

        }else{
            map.put("code", 1);
            map.put("msg", "图片上传失败");
        }
        return map;
    }

    @Override
    public Result updateSetting(MultipartFile file, Setting setting,HttpServletRequest request) {
        if (file!=null){
            String fileName = setting.getLogo().substring(setting.getLogo().lastIndexOf('/')+1);
            FileUtil.deleteFile(request,fileName,BookConstants.SETTING_SAVE_PATH);
            String newFileName = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"_logo";
            FileResult fr = FileUtil.saveFileToPath(request,file, BookConstants.SETTING_SAVE_PATH,BookConstants.SETTING_MAPPING_URL,newFileName);
            if (fr!=null){
                setting.setLogo(fr.getHttpUrl());
            }
        }
        List<String> list = (List<String>) request.getSession().getAttribute("settingImage");
       if(list!=null){
           for (String s : list) {
               if (setting.getIntroduce().indexOf(s)>-1){
                   FileUtil.renameFile(BookConstants.SETTING_SAVE_PATH,s);
               }
           }
       }
        setting.setIntroduce(setting.getIntroduce().replace("_temp",""));
        setting.setIntroduce(setting.getIntroduce());
        if(baseMapper.updateById(setting)>0){
            return Result.createSuccess("修改成功");
        }
            return Result.createError("修改失败");
    }

    @Override
    public Setting loadSetting() {
        QueryWrapper<Setting> queryWrapper = new QueryWrapper<Setting>();
        queryWrapper.eq("id",1);
        Setting setting = baseMapper.selectOne(queryWrapper);
        return setting;
    }
}
